﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

using System.DirectoryServices.AccountManagement;
using System.Windows.Threading;


namespace mns
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        bool logvalid;

        List<int> tasksid;
        List<byte> DoneDelay; // 0 no action       1 done           2 delay
        List<int> Delayvalue;

        String Accountname = "Anca";
        String[] Tips = { "you can do or delay this task", "You have done this task just now", "You have delayed this task for " };

        

        public MainWindow(List<int> id)
        {           
           
            InitializeComponent();
            tasksid = id;
            InitialDoneDelay(id.Count);
            InitialDelayValue(id.Count);
            InitialLogStatus();

            // update data
            mns.logdataDataSetTableAdapters.NotificationTableAdapter NewAdaptor;
            NewAdaptor = new mns.logdataDataSetTableAdapters.NotificationTableAdapter();
            for(int i=0;i<tasksid.Count;i++)
            {
                
                NewAdaptor.Insert(DateTime.Now.ToString(), TaskManage.GetTask(tasksid[i]).name);
            }
    
        }

        void InitialDoneDelay(int Count)
        {
            DoneDelay = new List<byte>();            
            for(int i=0;i<Count;i++)
            {
                DoneDelay.Add(0);
            }
        }

        void InitialDelayValue(int Count)
        {
            Delayvalue = new List<int>();
            for(int i=0;i<Count;i++)
            {
                Delayvalue.Add(0);
            }
        }

        int GetDelayValue(int index)
        {
            return Delayvalue[index];
        }

        int SetDelayValue(int index,int value)
        {
            Delayvalue[index] = value;
            return value;
        }

        public int CheckDoneDelay(int index)
        {
            return DoneDelay[index];
        }

        int SetDoneDelay(int index,byte value)
        {
            DoneDelay[index] = value;
            return value;
        }

        void InitialLogStatus()
        {
            //logvalid = false;
            logvalid = true;
            LogInfoEnable(GetLogStatus()); //log on 
            TaskInfoIsEnable(GetLogStatus());//Task show
        }

        bool GetLogStatus()
        {
            return logvalid;
        }

        bool SetLogStatus(bool b)
        {
            if(b != logvalid)
            {
                logvalid = b;
                LogInfoEnable(GetLogStatus()); //log on 
                TaskInfoIsEnable(GetLogStatus());//Task show
                return true;
            }
            else
            {
                return false;
            }
        }



        

        void ShowTaskInforamtion()
        {
            if (TaskList.SelectedIndex != -1)
            {

                //
                UndoButton.IsEnabled &= (CheckDoneDelay(TaskList.SelectedIndex) == 0 ?false:true);
                DoneButton.IsEnabled &= (CheckDoneDelay(TaskList.SelectedIndex) == 0 ?true:false);
                DelayButton.IsEnabled &= (CheckDoneDelay(TaskList.SelectedIndex) == 0 ?true : false);  

                //
                TaskInfoBrowser.Navigate(TaskManage.GetTask(tasksid[TaskList.SelectedIndex]).htmlFilePath);//open the task's html file in the webbrower
                TaskTextBox.Clear();
                TaskTextBox.Text += (Accountname + ": ") + Tips[CheckDoneDelay(TaskList.SelectedIndex)];
                if(CheckDoneDelay(TaskList.SelectedIndex) == 2)
                {
                    TaskTextBox.Text += Convert.ToString(GetDelayValue(TaskList.SelectedIndex)) + " Minutes";
                }
                //
            }
            else 
            {
                UndoButton.IsEnabled &= false;
                DoneButton.IsEnabled &= false;
                DelayButton.IsEnabled &= false;
            }

        }

        void TaskInfoIsEnable(bool b)
        {

            TaskBorder.IsEnabled = logvalid;
            TaskInfoBrowser.IsEnabled = logvalid;
            TaskList.IsEnabled = logvalid;
            SubmitButton.IsEnabled = logvalid;
            UndoButton.IsEnabled = logvalid;
            DoneButton.IsEnabled = logvalid;
            DelayButton.IsEnabled = logvalid;

            //
            if(b)
            {
                //
                ShowTaskInforamtion();
            }  
            else
            {
                //
                TaskInfoBrowser.Navigate("about:blank");
                TaskTextBox.Clear();
                TaskList.Items.Clear();
            }

        }



        void LogInfoEnable(bool b)
        {
            if (b)
            {
                //logbutton.Content = "ChangeUser";
                for (int i = 0; i < tasksid.Count; i++)
                {
                    TaskList.Items.Insert(i, TaskManage.GetTask(tasksid[i]).name);
                }

            }
            else
            {
               // logbutton.Content = "Connect";
               // UserTextBox.Clear();
              //  PsBox.Clear();
                TaskList.Items.Clear();
            }
            //UserTextBox.IsEnabled = !b;
           // PsBox.IsEnabled = !b;
            TaskList.IsEnabled = b;
        }


        private bool CheckAccount(string username,string password)
        {   
            var pc = new PrincipalContext(ContextType.Machine);
            try
            {
                if (pc.ValidateCredentials(username, password))
                {
                    Accountname = username;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (PrincipalOperationException poe)
            {
                if (poe.ErrorCode == 0x01)
                {
                    MessageBox.Show("somthing exceptional happens");
                }
                return false;
            }
            
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //
            //if(!logvalid)
            //{
            //    if (CheckAccount(UserTextBox.Text, PsBox.Password))
            //    {
            //        SetLogStatus(true);
            //        TaskList.Focus();
            //    }
            //    else
            //    {
            //        MessageBox.Show("The username and password are wrong, please input them again");
            //        UserTextBox.Clear();
            //        PsBox.Clear();
            //        UserTextBox.Focus();
            //    }
            //}
            //else
            //{
            //    SetLogStatus(false);
            //    UserTextBox.Focus();
            //}
   
        }


        private void TaskList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TaskInfoIsEnable(GetLogStatus());     
        }

        private void DoneButton_Click(object sender, RoutedEventArgs e)
        {
            if(TaskList.SelectedIndex!=-1)
            {
                SetDoneDelay(TaskList.SelectedIndex,1);
                TaskInfoIsEnable(GetLogStatus());
            }    
        }

        private void DelayButton_Click(object sender, RoutedEventArgs e)
        {
            if (TaskList.SelectedIndex != -1)
            {
                OneParameterInputDialog dlg = new OneParameterInputDialog("Delay", "Minitues", GetDelayValue(TaskList.SelectedIndex));
                Nullable<bool>ret = dlg.ShowDialog();
                if (ret.Value)
                {
                    SetDelayValue(TaskList.SelectedIndex, Convert.ToInt32(dlg.param.Text));
                    SetDoneDelay(TaskList.SelectedIndex, 2);
                    TaskInfoIsEnable(GetLogStatus());
                } 
  
            }  
        }

        private void UndoButton_Click(object sender, RoutedEventArgs e)
        {
            if (TaskList.SelectedIndex != -1)
            {
                SetDoneDelay(TaskList.SelectedIndex, 0);
                TaskInfoIsEnable(GetLogStatus());
            }
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            mns.logdataDataSetTableAdapters.OperationTableAdapter NewAdaptor;
            NewAdaptor = new mns.logdataDataSetTableAdapters.OperationTableAdapter();

            if(this.DialogResult.Value)
            {
                for (int i = 0; i < tasksid.Count; i++)
                {
                    switch (CheckDoneDelay(i))
                    {
                        case 0:     // do nothing
                            break;
                        case 1:     // done
                            TaskManage.GetTask(tasksid[i]).Done();
                            //update the local 
                            NewAdaptor.Insert(DateTime.Now.ToString(), TaskManage.GetTask(tasksid[i]).name, "yes", "no", "no");
                            break;
                        case 2:     //delay
                            TaskManage.GetTask(tasksid[i]).Delay(GetDelayValue(i));
                            NewAdaptor.Insert(DateTime.Now.ToString(), TaskManage.GetTask(tasksid[i]).name, "no", DateTime.Now.AddMinutes(GetDelayValue(i)).ToString(), "no");
                            break;
                    }
                }
            }

        }
   
       
    }
    
}
